import {
  queryListData,
  queryGetData,
  addData,
  updateBasicData,
  updateContentData,
  setRecommend,
  setNew,
  setGift,
  removeData,
} from '../services/product';
import { pretreatmentRemotePageListData, pretreatmentRemoteSingleData } from '../utils/tools';

export default {
  namespace: 'product',

  state: {},

  effects: {
    *list({ payload }, { call, put }) {
      const response = yield call(queryListData, payload);
      yield put({
        type: 'handleListData',
        payload: response,
      });
    },
    *get({ payload }, { call, put }) {
      const response = yield call(queryGetData, payload);
      yield put({
        type: 'handleGet',
        payload: response,
      });
    },
    *add({ payload }, { call, put }) {
      const response = yield call(addData, payload);
      yield put({
        type: 'handleAdd',
        payload: response,
      });
    },
    *updateBasic({ payload }, { call, put }) {
      const response = yield call(updateBasicData, payload);
      yield put({
        type: 'handleUpdateBasic',
        payload: response,
      });
    },
    *updateContent({ payload }, { call, put }) {
      const response = yield call(updateContentData, payload);
      yield put({
        type: 'handleUpdateContent',
        payload: response,
      });
    },
    *setRecommend({ payload }, { call, put }) {
      const response = yield call(setRecommend, payload);
      yield put({
        type: 'handleSetRecommend',
        payload: response,
      });
    },
    *setNew({ payload }, { call, put }) {
      const response = yield call(setNew, payload);
      yield put({
        type: 'handleSetNew',
        payload: response,
      });
    },
    *setGift({ payload }, { call, put }) {
      const response = yield call(setGift, payload);
      yield put({
        type: 'handleSetGift',
        payload: response,
      });
    },
    *remove({ payload }, { call, put }) {
      const response = yield call(removeData, payload);
      yield put({
        type: 'handleRemove',
        payload: response,
      });
    },
  },

  reducers: {
    handleListData(state, action) {
      const d = action.payload;
      const v = pretreatmentRemotePageListData(d);

      return {
        ...state,
        data: v,
      };
    },
    handleGet(state, action) {
      const d = action.payload;
      const v = pretreatmentRemoteSingleData(d);

      return {
        ...state,
        data: v,
      };
    },
    handleAdd(state, action) {
      const d = action.payload;
      const v = pretreatmentRemoteSingleData(d);

      return {
        ...state,
        data: v,
      };
    },
    handleUpdateBasic(state, action) {
      const d = action.payload;
      const v = pretreatmentRemoteSingleData(d);

      return {
        ...state,
        data: v,
      };
    },
    handleUpdateContent(state, action) {
      const d = action.payload;
      const v = pretreatmentRemoteSingleData(d);

      return {
        ...state,
        data: v,
      };
    },
    handleSetRecommend(state, action) {
      const d = action.payload;
      const v = pretreatmentRemoteSingleData(d);

      return {
        ...state,
        data: v,
      };
    },
    handleSetNew(state, action) {
      const d = action.payload;
      const v = pretreatmentRemoteSingleData(d);

      return {
        ...state,
        data: v,
      };
    },
    handleSetGift(state, action) {
      const d = action.payload;
      const v = pretreatmentRemoteSingleData(d);

      return {
        ...state,
        data: v,
      };
    },
    handleRemove(state, action) {
      const d = action.payload;
      const v = pretreatmentRemoteSingleData(d);

      return {
        ...state,
        data: v,
      };
    },
  },
};
